import abc
from copy import deepcopy
import logging

from pubsub.utils import ContextAdapter

logger = logging.getLogger('pubsub')


class BaseBroker(abc.ABC):
    def __init__(self, connection, tag, log_context=None):
        if callable(connection):
            self.connection = connection()
        else:
            self.connection = connection
        self.tag = tag
        self.log_context = deepcopy(log_context) or {}
        self.logger = ContextAdapter(logger, self.log_context)

    @abc.abstractmethod
    async def start_publish(self):
        pass

    @abc.abstractmethod
    async def publish(self, message, *args, **kwargs):
        pass

    @abc.abstractmethod
    async def start_consume(self, channels_subscribed, callback):
        pass

    @abc.abstractmethod
    async def stop(self, channels, message):
        pass
